<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/10
 * Time: 9:49
 */

namespace App\Http\Controllers\Setting;


use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Model\YearModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class YearController extends Controller
{
    /**
     * @return \Illuminate\Http\Response
     */
    public function lists()
    {
        $yearModel = new YearModel();
        $this->data = $yearModel->lists();
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function info(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取工程年度参数失败',
            'id.integer' => '工程年度参数类型错误',
        ];
        $param = $request->only('id');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $yearModel = new YearModel();
            $this->data = $yearModel->info($param);
        } else {
            throw new ApiException($validator->errors()->first(), 220101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function add(Request $request)
    {
        $rules = [
            'name' => 'required',
            'startTime' => 'required|date_format:Y-m-d',
            'endTime' => 'required|date_format:Y-m-d|after:startTime',
        ];
        $message = [
            'name.required' => '请输入工程年度名称',
            'startTime.required' => '请输入工程年度起始时间',
            'startTime.date_format' => '工程年度起始时间',
            'endTime.required' => '请输入工程年度截止时间',
            'endTime.date_format' => '工程年度截止时间',
            'endTime.after' => '工程年度截止时间必须大于工程年度起始时间',
        ];
        $param = $request->all();
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $yearModel = new YearModel();
            $checkYear = $yearModel->checkYear($param['startTime']);
            if ($checkYear != 0) {
                throw new ApiException('新年度起始时间不能小于已有年度的截止时间', 220202);
            }
            $result = $yearModel->insert($param);
            if (!$result) {
                throw new ApiException('保存失败，请稍后重试', 220202);
            }
        } else {
            throw new ApiException($validator->errors()->first(), 220201);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function edit(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
            'name' => 'required',
            'startTime' => 'required|date_format:Y-m-d',
            'endTime' => 'required|date_format:Y-m-d|after:startTime',
        ];
        $message = [
            'id.required' => '获取工程年度参数失败',
            'id.integer' => '工程年度参数类型错误',
            'name.required' => '请输入工程年度名称',
            'startTime.required' => '请输入工程年度起始时间',
            'startTime.date_format' => '工程年度起始时间',
            'endTime.required' => '请输入工程年度截止时间',
            'endTime.date_format' => '工程年度截止时间',
            'endTime.after' => '工程年度截止时间必须大于工程年度起始时间',
        ];
        $param = $request->all();
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $yearModel = new YearModel();
            $updateData = [
                'name' => $param['name'],
                'startTime' => $param['startTime'] . ' 00:00:00',
                'endTime' => $param['endTime'] . ' 23:59:59',
            ];
            $yearModel->update(['id'=>$param['id']],$updateData);
        } else {
            throw new ApiException($validator->errors()->first(), 220301);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function delete(Request $request){
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取工程年度参数失败',
            'id.integer' => '工程年度参数类型错误',
        ];
        $param = $request->only('id');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $yearModel = new YearModel();
            $yearModel->delete($param);
        } else {
            throw new ApiException($validator->errors()->first(), 220401);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }
}